﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZGSharp.Layers
{
    public class Sigmoid : Tensor
	{
		public static Tensor sigmoid(Tensor obj)
		{
			bool flag = obj == null;
			Tensor result;
			if (flag)
			{
				result = null;
			}
			else
			{
				Sigmoid tensor = new Sigmoid();
				tensor.kind = TensorKinds.LayerSigmoid;
				if (obj.kind == TensorKinds.TensorNormal || obj.build_only)
				{
					tensor.build_only = true;
					tensor.input_tensor = obj;
					obj.output_tensor.Add(tensor);
					return tensor;
				}
				tensor.build_only = false;
				tensor.Construct(obj.shape);
				tensor.PredInit(obj);
				Parallel.For(0, tensor.batch_size * tensor.value_len, delegate (int i)
				{
					tensor.outputs[i] = 1 / (Math.Exp(-1 * obj.outputs[i]) + 1);
				});
				result = tensor;
			}
			return result;
		}

		public override void Backward(Tensor back, double lr)
		{
			base.BackwordInit(null);
			//梯度使用累加的方式
			Parallel.For(0, this.batch_size * this.value_len, delegate (int i)
			{
				input_tensor.gradient[i] += gradient[i] * outputs[i] * (1 - outputs[i]);
			});
			this.output_tensor.Remove(back);
			bool flag = this.output_tensor.Count == 0;
			if (flag)
			{
				this.input_tensor.Backward(this, lr);
				Array.Clear(this.gradient, 0, this.gradient.Length);
			}
		}
	}
}
